﻿using System.IO;
using NUnit.Framework;
using System;

namespace openxmlconv
{
    [TestFixture]
    public class OdfParserFixture
    {
        [Test]
        public void TestSingleParagraphDocument()
        {
            FileStream file =  new FileStream("test/content1.xml", FileMode.Open, FileAccess.Read);
            Document DocumentParsed = OdfParser.Parse(file);
            Document DocumentProgrammed = new Document();
            DocumentProgrammed.AddParagraph(new Paragraph("Hello world!"));
            Assert.AreEqual(DocumentParsed, DocumentProgrammed);
        }
        [Test]
        public void TestMultiParagraphDocument()
        {
            FileStream file = new FileStream("test/content2.xml", FileMode.Open, FileAccess.Read);
            Document DocumentParsed = OdfParser.Parse(file);
            Document DocumentProgrammed = new Document();
            DocumentProgrammed.AddParagraph(new Paragraph("This"));
            DocumentProgrammed.AddParagraph(new Paragraph("Is"));
            DocumentProgrammed.AddParagraph(new Paragraph("A"));
            DocumentProgrammed.AddParagraph(new Paragraph("MultiParagraph Test"));
            Assert.AreEqual(DocumentParsed, DocumentProgrammed);
        }
        [Test]
        public void TestStyledParagraphDocument()
        {
            FileStream file = new FileStream("test/content3.xml", FileMode.Open, FileAccess.Read);
            Document DocumentParsed = OdfParser.Parse(file);
            Assert.IsNotNull(DocumentParsed.GetStyleTable().Query("P1"));
            foreach (Paragraph p in DocumentParsed)
            {
                Assert.IsNotNull(p.getStyle());
            }
        }
        [Test]
        public void TestBoldDocument()
        {
            FileStream file = new FileStream("test/content3.xml", FileMode.Open, FileAccess.Read);
            Document DocumentParsed = OdfParser.Parse(file);
            Assert.IsNotNull(DocumentParsed.GetStyleTable().Query("P1"));
            foreach (Paragraph p in DocumentParsed)
            {
                Assert.IsTrue(p.getStyle().getBold());
            }
        }
        [Test]
        public void TestItalicDocument()
        {
            FileStream file = new FileStream("test/content4.xml", FileMode.Open, FileAccess.Read);
            Document DocumentParsed = OdfParser.Parse(file);
            Assert.IsNotNull(DocumentParsed.GetStyleTable().Query("P1"));
            foreach (Paragraph p in DocumentParsed)
            {
                Assert.IsTrue(p.getStyle().getItalic());
            }
        }
        [Test]
        public void TestNoParagraphBug()
        {
            FileStream file = new FileStream("test/content3.xml", FileMode.Open, FileAccess.Read);
            Document DocumentParsed = OdfParser.Parse(file);
            Assert.IsNotNull(DocumentParsed.GetStyleTable().Query("P1"));
            foreach (Paragraph p in DocumentParsed)
            {
                return;
            }
            Assert.Fail();
        }
        [Test]
        public void TestUnderlinedDocument()
        {
            FileStream file = new FileStream("test/content5.xml", FileMode.Open, FileAccess.Read);
            Document DocumentParsed = OdfParser.Parse(file);
            Assert.IsNotNull(DocumentParsed.GetStyleTable().Query("P1"));
            foreach (Paragraph p in DocumentParsed)
            {
                Assert.IsTrue(p.getStyle().getUnderline());
            }
        }
        [Test]
        public void TestFontStyle()
        {
            FileStream file = new FileStream("test/content6.xml", FileMode.Open, FileAccess.Read);
            Document DocumentParsed = OdfParser.Parse(file);
            Assert.IsNotNull(DocumentParsed.GetStyleTable().Query("P1"));
            foreach (Paragraph p in DocumentParsed)
            {
                Assert.AreEqual(p.getStyle().getFontName(), "Arial");
            }
        }
        [Test]
        public void TestFontSize()
        {
            FileStream file = new FileStream("test/content7.xml", FileMode.Open, FileAccess.Read);
            Document DocumentParsed = OdfParser.Parse(file);
            Assert.IsNotNull(DocumentParsed.GetStyleTable().Query("P1"));
            foreach (Paragraph p in DocumentParsed)
            {
                Assert.AreEqual(p.getStyle().getFontSize(), 16);
            }
        }
        [Test]
        public void TestParagraphAlignment()
        {
            FileStream file = new FileStream("test/content8.xml", FileMode.Open, FileAccess.Read);
            Document DocumentParsed = OdfParser.Parse(file);
            foreach (Paragraph p in DocumentParsed)
            {                
                Assert.AreEqual(p.getStyle().getAlignment(), "end");                              
            }
            
        }
        [Test]
        public void TestRunStyle()
        {
            FileStream file = new FileStream("test/content9.xml", FileMode.Open, FileAccess.Read);
            Document DocumentParsed = OdfParser.Parse(file);
            int i = 0;
            foreach (Paragraph p in DocumentParsed)
            {
                Console.WriteLine(p.GetRuns().Count);     
                foreach (Run r in p)
                {
                    Console.WriteLine("aqui" + r.getText());       
                    if (i == 1)
                    {
                                             
                        Assert.IsTrue(r.getStyle().getBold());
                    }
                    else if (i == 3)
                    {                        
                        Assert.IsTrue(r.getStyle().getItalic());
                    }
                    i++;
                }
                
            }
            Console.WriteLine(i);
            if (i != 5)
                Assert.Fail();
        }

        
    }
}
